package cn.tedu.weibo2.controller;

import cn.tedu.weibo2.mapper.WeiboMapper;
import cn.tedu.weibo2.pojo.dto.WeiboDto;
import cn.tedu.weibo2.pojo.entity.Weibo;
import cn.tedu.weibo2.pojo.vo.UserVo;
import cn.tedu.weibo2.pojo.vo.WeiboDetailVo;
import cn.tedu.weibo2.pojo.vo.WeiboIndexVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class WeiboController {
    @Autowired(required = false)
    WeiboMapper mapper;
    @RequestMapping("/weibo/insert")
    public int insert(@RequestBody WeiboDto weibo, HttpSession session){
        UserVo user = (UserVo) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        Weibo w = new Weibo();
        BeanUtils.copyProperties(weibo,w);
        w.setUserId(user.getId());
        w.setCreated(new Date());

        mapper.insert(w);
        return 1;
    }
    @RequestMapping("/weibo/select")
    public List<WeiboIndexVo> select(){
        return mapper.select();
    }
    @RequestMapping("/weibo/selectById")
    public WeiboDetailVo selectById(int id){
        return mapper.selectById(id);
    }

}
